package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.request.SearchStockRequest;

import java.io.Serializable;

/**
 * 库存查询
 */
public class RepertorySearchActivity extends BaseActivity
{
	@BindView(R.id.txt_search) TextView txtSearch;

	@BindView(R.id.txt_custom) TextView txtCustom;

	@BindView(R.id.txt_factory) TextView txtFactory;

	@BindView(R.id.txt_kw) TextView txtKLbs;
	@BindView(R.id.txt_ck) TextView txtCk;
	@BindView(R.id.txt_wlcode) TextView txtWlCode;
	@BindView(R.id.txt_wlName) TextView txtWlName;
	@BindView(R.id.txt_work) TextView txtWork;
	private SelectDictionaryBean customBean, companyBean, factoryBean, wuLiaoBean;
	private SelectDictionaryBean ckBean, cuLbs;
	public static final int SEARCH_CUSTOM_KEY = 101;  //客户选择
	public static final int SEARCH_FACTORY_KEY = 102;  //公司
	public static final int SEARCH_WORK_KEY = 103;  //工厂
	public static final int SEARCH_CK_KEY = 104;  //仓库
	public static final int SEARCH_CKLBS_KEY = 105;  //库位
	public static final int SEARCH_WL_KEY = 106;  //物料

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_kucun_search;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getString(R.string.inventory_query));
		showLeftBack();
		txtSearch.setOnClickListener(this);
		txtCustom.setOnClickListener(this);
		txtFactory.setOnClickListener(this);
		txtKLbs.setOnClickListener(this);
		txtCk.setOnClickListener(this);
		txtWlCode.setOnClickListener(this);
		txtWlName.setOnClickListener(this);
		txtWork.setOnClickListener(this);
	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{
		case R.id.txt_search:
			Intent it = new Intent(RepertorySearchActivity.this, DepotSelectActivity.class);
			SearchStockRequest searchStockRequest = new SearchStockRequest();
			if (customBean != null) {
				searchStockRequest.customerId = customBean.getValue();
			}
			if (companyBean != null) {
				searchStockRequest.companyId = companyBean.getValue();
			}
			if (factoryBean != null) {
				searchStockRequest.factoryId = factoryBean.getValue();
			}
			if (ckBean != null) {
				searchStockRequest.whId = ckBean.getValue();
			}
			if (cuLbs != null) {
				searchStockRequest.stockId = cuLbs.getValue();
			}
			if (wuLiaoBean != null) {
				searchStockRequest.skuId = wuLiaoBean.getValue();
			}
			it.putExtra("searchBean", (Serializable) searchStockRequest);
			it.putExtra("tilteName", "库存查询");
			startActivity(it);
			break;

		case R.id.txt_custom:
			Intent intent = new Intent(this, SelectBaseDataActivity.class);
			intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_CUSTOM);
			startActivityForResult(intent, SEARCH_CUSTOM_KEY);
			break;
		case R.id.txt_factory:
			Intent intentf = new Intent(this, SelectBaseDataActivity.class);
			intentf.putExtra("action", AppConfig.BaseDataAction.Company);
			startActivityForResult(intentf, SEARCH_FACTORY_KEY);
			break;
		case R.id.txt_ck:
			Intent intentc = new Intent(this, SelectBaseDataActivity.class);
			intentc.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE);
			startActivityForResult(intentc, SEARCH_CK_KEY);
			break;
		case R.id.txt_kw:
			if (ckBean == null) {
				showToast("请先选择仓库");
				return;
			}
			Intent intentkw = new Intent(this, SelectBaseDataActivity.class);
			intentkw.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_STOCK_POSITION);
			intentkw.putExtra("whId", ckBean.getValue());
			startActivityForResult(intentkw, SEARCH_CKLBS_KEY);
			break;
		case R.id.txt_work:
			Intent intentw = new Intent(this, SelectBaseDataActivity.class);
			intentw.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WORKSHOP);
			startActivityForResult(intentw, SEARCH_WORK_KEY);
			break;
		case R.id.txt_wlcode:
			Intent intent1 = new Intent(this, MaterialSelectActivity.class);
			startActivityForResult(intent1, SEARCH_WL_KEY);
			break;
//		case R.id.txt_wlName:
//
//			break;
		}
	}

	@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null)
		{
			switch (requestCode)
			{
			case SEARCH_CUSTOM_KEY:
				customBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				txtCustom.setText(customBean.getText());
				break;
			case SEARCH_FACTORY_KEY:  //公司
				companyBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				txtFactory.setText(companyBean.getText());
				break;
			case SEARCH_WORK_KEY:  //工厂
				factoryBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				txtWork.setText(factoryBean.getText());
				break;
			case SEARCH_CK_KEY: //仓库
				ckBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				if (ckBean == null)
					return;
				txtCk.setText(ckBean.getText());
				break;
			case SEARCH_CKLBS_KEY:  //库位
				cuLbs = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				if (cuLbs == null)
					return;
				txtKLbs.setText(cuLbs.getText());
				break;
			case SEARCH_WL_KEY: //物料
				wuLiaoBean = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				if (wuLiaoBean == null)
					return;
				txtWlCode.setText(wuLiaoBean.getCode());
				txtWlName.setText(wuLiaoBean.getText());
				break;
			}

		}
	}
}
